package SelectionSort;

/**
 * ClassName: SortingHelper
 * Package: SelectionSort
 *
 * @Author 马学兴
 * @Create 2023/11/29 17:41
 * @Version 1.0
 * Description:
 */
public class SortingHelper {
    private SortingHelper(){}
    public static<E extends Comparable> boolean isSorted(E [] arr){
        for (int i = 1; i < arr.length; i++) {
            if (arr[i-1].compareTo(arr[i])>0){
                return false;
            }
        }
        return true;

    }
    public static <E extends Comparable> void sortTest(String sortname,E[] arr){
        long startTime = System.nanoTime();//计时-时间戳ns
        if (sortname.equals("SelectionSort")){
            SelectionSort.sort(arr);
            SelectionSort.sort2(arr);
        } else if (sortname.equals("InsertionSort")) {
            InsertionSort.sort(arr);
        }else if (sortname.equals("InsertionSort2")) {
            InsertionSort.sort2(arr);
        }else if (sortname.equals("InsertionSort3")) {
            InsertionSort.sort3(arr);
        }
        long endTime = System.nanoTime();
        double time=(endTime-startTime)/ 1000000000.0;
        if (!SortingHelper.isSorted(arr)){//判断是否真的有序
            throw new RuntimeException(sortname+" failed");
        }
        System.out.println(String.format("%s , n=%d :%f s",sortname,arr.length,time));
    }
}
